//
//  QBStatusOriginalView.swift
//  Wei91BoiOS
//
//  Created by bing on 16/3/25.
//  Copyright © 2016年 bing. All rights reserved.
//

import UIKit

import SnapKit

class QBStatusOriginalView: UIView {
    
    /// 底部约束的属性
    var originalViewBottomCons: Constraint?
    
    // MARK: - 属性
    var status: QBStatus? {
        
        didSet {
            // 当别人设置微博模型的时候，我们根据微博模型来设置原创为欧巴的相关内容
            /// 1.设置头像
            if let imageUrl = status?.user?.profile_image_url {
                let url = NSURL(string: imageUrl)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            /// 设置名称
            nameLabel.text = status?.user?.screen_name
            
            /// 设置时间
            //timeLabel.text = status?.created_at
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "刚刚").sinaDateDescription()
            
            /// 来源
            sourceLabel.text = status?.source
            
            /// 会员等级
            memberView.image = status?.user?.mbrankImage
            
            /// 认证图标
            verifiedView.image = status?.user?.verified_typeImage
            /// 设置微博内容
            //contentLabel.text = status?.text
            contentLabel.attributedText = status?.attrText
            
          // 判断微博是否要显示图片,要显示图片才显示QBStatusPictureView,如果不要显示就隐藏掉
          // 判断是否要显示图片,先判断pic_urls这个可选是否有值,如果有值还要判断数量是否>0
          // 添加约束之前删除这个约束
          originalViewBottomCons?.uninstall() // 删除
           
            if let pictureURLs = status?.storePictureURLs {
                // 有图片需要显示
                pictureView.hidden = false
                
                // 给picureView赋值pictureURLs数组
                pictureView.pictureURLs = pictureURLs
                // 需要修改约束，不然还会一直参考pictureView
                // 有图片需要显示QBStatusOriginalView底部
                // 使用snp_updateConstraints不能复用
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.originalViewBottomCons = make.bottom.equalTo(pictureView).offset(QBStatusCellMargin).constraint
                })
            } else {
                // 没有图片需要显示
                pictureView.hidden = true
                // 没有图片需要显示QBStatusOriginalView的底部就参照conetntLabel的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.originalViewBottomCons = make.bottom.equalTo(contentLabel).offset(QBStatusCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }

    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(QBStatusCellMargin)
            make.top.equalTo(self).offset(QBStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(QBStatusCellMargin)
            make.top.equalTo(iconView.snp_top)
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(QBStatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(QBStatusCellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        
        /// 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.leading.equalTo(nameLabel.snp_trailing).offset(QBStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        /// 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView.snp_bottom).offset(QBStatusCellMargin)
            make.leading.equalTo(self).offset(QBStatusCellMargin)
            make.trailing.equalTo(self).offset(-QBStatusCellMargin)
        }
        
        /// 配图视图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(QBStatusCellMargin)
            make.leading.equalTo(self).offset(QBStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        /// 原创cell的底部参照pictureView的底部
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottomCons = make.bottom.equalTo(pictureView).offset(QBStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()

        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(QBStatusCellContentSize)
        
        return label
    }()
    
    /// 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()

        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(QBStatusCellBottomDefaultFontSize)
        
        return label
    }()
    
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()

        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(QBStatusCellBottomDefaultFontSize)
        
        return label
    }()
    
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView()
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView()
    
    /// 原创微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
       
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(QBStatusCellContentSize)
        label.numberOfLines = 0
        
        // 换行/折行宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * QBStatusCellMargin
        
        return label
    }()
    
    /// 配图视图,不管cell是否要显示图片,先加载进来,如果模型里面不需要显示图片,就把这个配图视图隐藏掉,如果要显示图片就把这个配图视图显示出来
    private lazy var pictureView: QBStatusPictureView = QBStatusPictureView()
}